package com.xky.sportstestdemo01.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.xky.sportstestdemo01.common.Result;
import com.xky.sportstestdemo01.pojo.entity.PhysicalFitness;
import com.xky.sportstestdemo01.pojo.entity.PhysicalFitnessScores;
import com.xky.sportstestdemo01.service.PhysicalFitnessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.dao.DuplicateKeyException;

import java.util.List;

/**
 * 体测成绩表(PhysicalFitness)表控制层
 *
 * @author twt
 * @since 2024-11-08 13:49:44
 */
@CrossOrigin
@RestController
@RequestMapping("physicalFitness")
public class PhysicalFitnessController {

    @Autowired
    PhysicalFitnessService physicalFitnessService;


    /**
     * 查询全部
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<PhysicalFitness> physicalFitnessList = physicalFitnessService.list(new QueryWrapper<PhysicalFitness>().orderByDesc("physicalFitnessId"));
        return Result.success(physicalFitnessList);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody PhysicalFitness physicalFitness) {
        try {
            physicalFitnessService.save(physicalFitness);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result update(@RequestBody PhysicalFitness physicalFitness) {
        physicalFitnessService.updateById(physicalFitness);
        return Result.success();
    }

    /**
     * 删除
     */

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        physicalFitnessService.removeById(id);
        return Result.success();
    }

    /**
     * 根据ID查询信息
     *
     * @author 唐文涛
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        PhysicalFitness physicalFitness = physicalFitnessService.getById(id);
        return Result.success(physicalFitness);
    }

    /**
     * 根据学生姓名查询信息
     *
     * @auther 唐文涛
     */
    @GetMapping("/selectByName/{name}")
    public Result selectByName(@PathVariable String name) {
        PhysicalFitness physicalFitness = physicalFitnessService.getOne(new QueryWrapper<PhysicalFitness>().eq("name", name));
        return Result.success(physicalFitness);
    }

    /**
     * 多条件模糊查询信息
     * pageNum 当前的页码
     * pageSize 每页查询的个数
     *
     * @author 唐文涛
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize
    ) {
        //TODO
        return Result.success();
    }

    /**
     * 计算成绩
     *
     * @auther 唐文涛
     */
    @PostMapping("/calculate-scores")
    public PhysicalFitnessScores calculateScores(@RequestParam Integer studentId) {
        return physicalFitnessService.calculateScores(studentId);
    }
}


